package com.redlion.alt;

import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.math.MathUtils;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.redlion.Game;

public abstract class AbstractShot {
	public float MAX_LIVE_TIME = 0.4f;
	public float aliveTime = 0;
	public Vector2 start = new Vector2();
	public Vector2 end = new Vector2();
	public Vector2 dir = new Vector2();
	public float centerX;
	public float centerY;
	public Sprite sprite;
	public Body body;
	public float raduis;

	public AbstractShot(Vector2 start, Vector2 dir) {
		this.start.set(start);
		this.dir.set(dir);
	}

	public void createSprite(ITextureRegion textureRegion) {
		sprite = new Sprite(start.x-(textureRegion.getWidth()/2),start.y-(textureRegion.getHeight()/2), textureRegion,
				Game.gameActivity.getVertexBufferObjectManager());
	}

	public void createBody(PhysicsWorld physicsWorld, FixtureDef fixtureDef) {
		body = PhysicsFactory.createCircleBody(physicsWorld, sprite,
				BodyType.DynamicBody, fixtureDef);
	}

	public void update(float delta) {
		aliveTime += delta;
	}

	public void dir(float angle) {
		float degAngle = MathUtils.degToRad(angle);
		dir = new Vector2((float) Math.cos(degAngle),
				(float) Math.sin(degAngle)).nor();
	}

	public void dir(Vector2 end) {
		this.end.set(end);
		calcDir();
	}

	public void dir(Vector2 start, Vector2 end) {
		this.start.set(start);
		this.end.set(end);
		calcDir();
	}

	private void calcDir() {
		dir = (start.cpy().add(end)).nor();
	}

	public void shoot(float multiplier) {
		body.setBullet(true);
		body.applyLinearImpulse(dir.mul(multiplier), start);
	}
}
